package com.hzpd.hainan.module.news.activity;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.hzpd.hainan.R;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.custorm.editcolumn.DragGridView;
import com.hzpd.hainan.model.event.ChannelSortedList;
import com.hzpd.hainan.model.news.NewsChannelBean;
import com.hzpd.hainan.module.news.adapter.DragAdapter;
import com.hzpd.hainan.utils.LogUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;


/**
 * Created by 猪猪侠 on 2017/7/3.
 */

public class MyEditColumnActivity extends ToolBarActivity {


    @BindView(R.id.choise_sv)
    DragGridView choiseSv;
    @BindView(R.id.nochoise_sv_1)
    DragGridView nochoiseSv1;

    @BindView(R.id.mytopic_tv)
    TextView mytopic_tv;
    @BindView(R.id.click_delete_tv)
    TextView click_delete_tv;
    @BindView(R.id.tuijian_tv)
    TextView tuijian_tv;
    @BindView(R.id.click_add_tv)
    TextView click_add_tv;

    @Override
    public void initView() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            mytopic_tv.setText("我的主题");
            click_delete_tv.setText("点击删除主题");
            tuijian_tv.setText("推荐");
            click_add_tv.setText("点击添加到我的主题");
        }else{
            mytopic_tv.setText("བརྗོད་སྙིང་།");
            click_delete_tv.setText("བརྗོད་སྙིང་བསུབ་པ།");
            tuijian_tv.setText("འོས་སྦྱོར།");
            click_add_tv.setText("བརྗོད་སྙིང་དུ་འཇོག་པ།");
        }
    }

    private DragAdapter selectAdapter;	//上面的
    private DragAdapter unSelectAdapter0;//下面的适配器

    //适配器内容
    private List<NewsChannelBean> selectList;//上面的
    private List<NewsChannelBean> unSelectList0;//下面的
    private ChannelSortedList csl;
    @Override
    public String initToolBarTitle() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            return "栏目订阅";
        }else{
            return "ལེ་ཚན་སྔོན་མངག";
        }
    }
    @Override
    public int setMyContentView() {
        return R.layout.editcolumn_my_layout;
    }
    @Override
    public void initData() {
        csl=new ChannelSortedList();
        selectList = new ArrayList<>();
        unSelectList0 = new ArrayList<>();
        selectList = DataSupport.where("ischoice=?","1").order("sortOrder").find(NewsChannelBean.class);
        LogUtils.i("selectList-->"+selectList.size());
        unSelectList0 = DataSupport.where("ischoice=?","0").order("sortOrder").find(NewsChannelBean.class);
        LogUtils.i("unSelectList0-->"+unSelectList0.size());
        setUpView();

    }
    private void setUpView(){
        unSelectAdapter0=new DragAdapter(this,unSelectList0,1);
        selectAdapter=new DragAdapter(this, selectList,0);
        choiseSv.setAdapter(selectAdapter);//绿色的
        nochoiseSv1.setAdapter(unSelectAdapter0);
        choiseSv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    final int position, long id) {
                if(position == 0|position==1){
                    return;
                }
                final NewsChannelBean ncb = selectAdapter.getAdapterData().get(position);
                LogUtils.i("category-->"+ncb.getCnname());
                selectAdapter.deleteDataAtPosition(position);
                 unSelectAdapter0.addDataAtPosition(ncb,0);
                ContentValues values = new ContentValues();
                values.put("ischoice","0");
                DataSupport.updateAll(NewsChannelBean.class, values, "tid=?",  ncb.getTid());
            }
        });
        nochoiseSv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    final int position, long id) {
                LogUtils.i("position-->" + position);

                final NewsChannelBean ncb = unSelectAdapter0.getAdapterData().get(position );
                LogUtils.i("category-->"+ncb.getCnname());
                unSelectAdapter0.deleteDataAtPosition(position);
                selectAdapter.addDataAtPosition(ncb,-1);
                ContentValues values = new ContentValues();
                values.put("ischoice","1");
                DataSupport.updateAll(NewsChannelBean.class, values, "tid=?",  ncb.getTid());
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
    @Override
    protected void onDestroy() {
        //选中的标题
        List<NewsChannelBean> list = selectAdapter.getAdapterData();
        for (int i = 0; i < list.size(); i++) {
            ContentValues values = new ContentValues();
            values.put("sortOrder",i);
            DataSupport.updateAll(NewsChannelBean.class, values, "tid=?",  list.get(i).getTid());
        }
        csl.setSaveTitleList(list);
        //未选中的标题推荐
        List<NewsChannelBean> list0 = unSelectAdapter0.getAdapterData();
        for (int i = 0; i < list0.size(); i++) {
            ContentValues values = new ContentValues();
            values.put("sortOrder","1000"+i);
            DataSupport.updateAll(NewsChannelBean.class, values, "tid=?",  list0.get(i).getTid());
        }
        EventBus.getDefault().post(csl);
        super.onDestroy();
    }
}
